package com.hsmpay.mobile.service.user.impl;

import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.service.impl.MemcachedServiceSupport;
import com.hsmpay.common.util.system.Constant;
import com.hsmpay.mobile.dao.user.SendmsgLogDao;
import com.hsmpay.mobile.pojo.user.SendmsgLog;
import com.hsmpay.mobile.service.user.VerifiService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证接口 暂时不做保存
 *
 * @author 颜铃璋
 * @date 2013-1-28
 */
@Service("verifiService")
public class VerifiServiceImpl extends MemcachedServiceSupport<User, Long> implements VerifiService<User, Long> {

    @Resource(name = "sendmsgLogDao")
    private SendmsgLogDao<SendmsgLog, Long> sendmsgLogDao;

    public VerifiServiceImpl() {
        EXPIRATION_TIME = Integer.parseInt(Constant.getAttribute("REGISTER_CODE_MEMCACHE_EXPIRE"));
    }

    /**
     * 保存手机验证码 到memcache
     *
     * @param mobile
     * @param code
     * @return
     * @throws Exception
     */
    public Map<String, String> sendMobileCode(String mobile, String code, SendmsgLog sendmsgLog,String content) throws Exception {
        String key = new StringBuilder().append(REGISTER_CODE_KEY).append(mobile).toString();
        boolean flag = setCacheData(key, code);
        //发送短信
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("reqCode", "9992");
        resultMap.put("reqMsg", "网络异常！请5分钟后再发送短信！");
        if (flag) {
            //发送短信
            resultMap = sendMobileSM(mobile, code, sendmsgLog,content);
        }
        return resultMap;
    }

    /**
     * 从memcache中取出 手机验证码
     *
     * @param mobile
     * @return
     * @throws Exception
     */
    public String getRegisterCodeToMemcache(String mobile) throws Exception {
        String key = new StringBuilder().append(REGISTER_CODE_KEY).append(mobile).toString();
        return (String) getCacheData(key);
    }

    /**
     * 校验信息
     *
     * @param codeType
     * @param verifiType
     * @param codeOrPswd
     * @param mobile
     * @param payPawd
     * @return
     * @throws Exception
     */
    public boolean verifiCode(Integer codeType, Integer verifiType, String codeOrPswd, String mobile, String payPawd) throws Exception {
        String code = null;
        if (null == verifiType) return true;
        switch (codeType) {
            case 1://注册时  不能验证支付
                if (2 == verifiType) return true;
            default:
                code = getRegisterCodeToMemcache(mobile);
        }

        switch (verifiType) {
            case 0:
                return true;//不验证
            case 1: //手机验证码 验证
                if (codeOrPswd.equals(code))
                    return true;
                else
                    return false;
            case 2://支付密码验证
                if (codeOrPswd.equals(payPawd))
                    return true;
                else
                    return false;
            default:
                return true;
        }
    }

    /**
     * 发送短信
     *
     * @param mobile
     * @param code
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public Map<String, String> sendMobileSM(String mobile, String code, SendmsgLog sendmsgLog,String content) throws Exception {
        Map<String, String> resultMap = new HashMap<String, String>();
        //#####  调用短信接口
        StringBuilder sbf = new StringBuilder("http://esm2.9wtd.com:9001/sendXSms.do?");
        sbf.append("username=hsmzf");
        sbf.append("&password=335621");
        sbf.append("&productid=395052");
        sbf.append("&mobile="+mobile);
        sbf.append("&content="+ URLEncoder.encode(content, "utf-8"));
        URL url = new URL(sbf.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection .setRequestMethod("POST");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine  = in.readLine();
        //这里直接返回成功
        if(inputLine.substring(0,1).equals("1")){
            resultMap.put("reqCode", "0000");
            resultMap.put("reqMsg", "发送短信成功！");
        }else{
            resultMap.put("reqCode", "0001");
            resultMap.put("reqMsg", "发送短信失败！");
        }
        //插入日志
        sendmsgLog.setReqCode(resultMap.get("reqCode"));
        sendmsgLog.setReqMsg(resultMap.get("reqMsg"));
        sendmsgLogDao.insertEntity(sendmsgLog);

        return resultMap;
    }

    public static void main(String[] args) throws IOException {
        String content = "尊敬的客户您好,欢迎使用花生米,您的手机验证码为:[" + 100006 + "],感谢您的支持！";
        StringBuilder sbf = new StringBuilder("http://esm2.9wtd.com:9001/sendXSms.do?");
        sbf.append("username=hsmzf");
        sbf.append("&password=335621");
        sbf.append("&productid=395052");
        sbf.append("&mobile=15210183472");
        sbf.append("&content="+ URLEncoder.encode(content, "utf-8"));
        URL url = new URL(sbf.toString());
        Date date = new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String time = sdf.format(date);
        System.out.println(time);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection .setRequestMethod("POST");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine  = in.readLine();
        System.out.println(inputLine);
    }
}
